import type { MockMethod } from 'vite-plugin-mock'
import Mock from 'mockjs'

export interface Role {
  id: string
  code: string
  name: string
}

export interface Permission {
  id: string
  code: string
  name: string
}

export type UserListItemStatusKeyType = 'Active' | 'Pending' | 'Banned' | 'Rejected'
export interface User {
  id: string
  username: string
  address: string
  phone: string
  realname: string
  status: UserListItemStatusKeyType
  avatar: string
  email: string
  roles: Role[]
  permissions: Permission[]
}

const Random = Mock.Random
const mock = Mock.mock

const getRoles = (): Role[] => [
  {
    id: Random.guid(),
    code: 'manager',
    name: '管理'
  },
  {
    id: Random.guid(),
    code: 'operater',
    name: '运营'
  },
  {
    id: Random.guid(),
    code: 'financer',
    name: '财务'
  },
  {
    id: Random.guid(),
    code: 'developer',
    name: '开发'
  }
]

const getPermissions = (): Permission[] => [
  {
    id: Random.guid(),
    code: 'user.findAll',
    name: '用户列表'
  },
  {
    id: Random.guid(),
    code: 'role.findAll',
    name: '角色列表'
  },
  {
    id: Random.guid(),
    code: 'permission.findAll',
    name: '权限列表'
  },
  {
    id: Random.guid(),
    code: 'static-css-hover',
    name: 'Hover.css'
  },
  {
    id: Random.guid(),
    code: 'static-css-transition',
    name: '过渡效果'
  },
  {
    id: Random.guid(),
    code: 'static-css-loading',
    name: '加载效果'
  },
  {
    id: Random.guid(),
    code: 'test-vue-attrs',
    name: '测试vue的透传attrs'
  },
  {
    id: Random.guid(),
    code: 'test-vue-project-todo',
    name: 'todo项目'
  },
  {
    id: Random.guid(),
    code: 'user-add',
    name: '允许用户新增'
  },
  {
    id: Random.guid(),
    code: 'user-edit',
    name: '允许用户编辑'
  },
  {
    id: Random.guid(),
    code: 'test-vueuse-use-preferred-dark',
    name: '测试usePreferredDark'
  },
  {
    id: Random.guid(),
    code: 'test-vueuse-use-preferred-languages',
    name: '测试usePreferredLanguages'
  },
  {
    id: Random.guid(),
    code: 'test-vueuse-use-draggable',
    name: '测试useDraggable'
  }
]

const getUser = (): User => ({
  id: Random.guid(),
  address: Random.county(true),
  phone: `1${Random.string('number', 10)}`,
  realname: Random.cname(),
  email: Random.email(),
  ...mock({
    username: Random.string('lower', Random.integer(6, 18)),
    avatar() {
      return Random.image('100x100', Random.color(), Random.color(), this.username[0])
    },
    status() {
      const status = ['Active', 'Pending', 'Banned', 'Rejected']
      return status[Random.integer(0, status.length - 1)]
    },
    roles() {
      const roles = getRoles()
      return roles.slice(0, Random.integer(0, roles.length - 1))
    }
  })
})

const UserTableRows = new Array(101).fill(null).map(() => getUser())

export default [
  {
    url: '/mock-api/user/profile',
    method: 'get',
    response() {
      return {
        status: true,
        message: 'success',
        code: 0,
        data: {
          username: 'root',
          is_root: true,
          roles: getRoles().map((role) => role.code),
          permissions: getPermissions().map((role) => role.code)
        }
      }
    }
  },
  {
    url: '/mock-api/role/list',
    method: 'get',
    response() {
      return {
        status: true,
        message: 'success',
        code: 0,
        data: getRoles()
      }
    }
  },
  {
    /**
     * @deprecated 不再使用
     */
    url: '/mock-api/user/status',
    method: 'get',
    response() {
      return {
        status: true,
        message: 'success',
        code: 0,
        data: [
          {
            value: 'All',
            label: '所有',
            badgeColor: '#909399',
            badgeValue: UserTableRows.length
          },
          {
            value: 'Active',
            label: '活动',
            badgeColor: '#409eff',
            badgeValue: UserTableRows.filter((row) => row.status === 'Active').length
          },
          {
            value: 'Pending',
            label: '待定',
            badgeColor: '#e6a23c',
            badgeValue: UserTableRows.filter((row) => row.status === 'Pending').length
          },
          {
            value: 'Banned',
            label: '禁止',
            badgeColor: '#f56c6c',
            badgeValue: UserTableRows.filter((row) => row.status === 'Banned').length
          },
          {
            value: 'Rejected',
            label: '失效',
            badgeColor: '#f56c6c',
            badgeValue: UserTableRows.filter((row) => row.status === 'Rejected').length
          }
        ]
      }
    }
  },
  {
    url: '/mock-api/user/list',
    method: 'get',
    response({ query }) {
      const { page = 1, size = 20, status = 'All', roles = '', keyword = '' } = query
      const p = +page
      const s = +size
      let data = UserTableRows
      if (status !== 'All') {
        data = data.filter((row) => row.status === status)
      }
      if (roles) {
        const rs = roles.split(',') as string[]
        data = data.filter((row) => row.roles.find((role) => rs.includes(role.code)))
      }
      if (keyword) {
        data = data.filter(
          (row) =>
            row.username.indexOf(keyword) > -1 ||
            row.email.indexOf(keyword) > -1 ||
            row.address.indexOf(keyword) > -1 ||
            row.phone.indexOf(keyword) > -1 ||
            row.realname.indexOf(keyword) > -1
        )
      }
      return {
        status: true,
        message: 'success',
        code: 0,
        data: {
          total: data.length,
          page: p,
          size: s,
          status: {
            All: UserTableRows.length,
            Active: UserTableRows.filter((row) => row.status === 'Active').length,
            Pending: UserTableRows.filter((row) => row.status === 'Pending').length,
            Banned: UserTableRows.filter((row) => row.status === 'Banned').length,
            Rejected: UserTableRows.filter((row) => row.status === 'Rejected').length
          },
          data: data.slice((p - 1) * s, p * s)
        }
      }
    }
  },
  {
    url: '/mock-api/user/:id',
    method: 'delete',
    response() {
      return {
        status: true,
        message: 'success',
        code: 0
      }
    }
  },
  {
    url: '/mock-api/user',
    method: 'post',
    response({ body }) {
      body.id = Random.guid()
      return {
        status: true,
        message: 'success',
        code: 0,
        data: body
      }
    }
  },
  {
    url: '/mock-api/user/:id',
    method: 'put',
    response({ body }) {
      return {
        status: true,
        message: 'success',
        code: 0,
        data: body
      }
    }
  },
  {
    url: '/mock-api/user/:id',
    method: 'get',
    response() {
      // const id = url.replace('/mock-api/user/', '')
      return {
        status: true,
        message: 'success',
        code: 0,
        data: UserTableRows[0]
      }
    }
  }
] as MockMethod[]
